package com.bidevalution.business.job;

import com.bidevalution.business.enums.VoteAlgorithmEnum;
import com.bidevalution.business.enums.VoteTypeEnum;
import com.bidevalution.business.exception.CheckException;
import com.bidevalution.business.factory.CheckJobBase;
import com.bidevalution.business.vote.service.BeExpertVoteService;
import com.bidevalution.dao.entity.BeExpertVoteDetailExt;
import com.bidevalution.dao.entity.BeExpertVoteExt;
import com.bidevalution.dao.entity.CheckJob;
import com.bidevalution.dao.entity.VoteParams;
import com.bidevalution.dao.mapper.BeEvalutionOutcomeMapper;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.OConvertUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Log4j2
@Component
public class CompositeVoteCheckJob extends CheckJobBase {
    @Autowired
    private BeEvalutionOutcomeMapper beEvalutionOutcomeMapper;
    @Autowired
   private BeExpertVoteService beExpertVoteService;

    private static CompositeVoteCheckJob thisCheckJob;

    @PostConstruct
    public void init() {
        thisCheckJob = this;
    }
    @Override
    public void check(CheckJob checkJob) throws CheckException,Exception {
        log.info("LOG[]check[]{},params={}", "检查资信标打分是否一致",GsonUtil.toJsonWithGson(checkJob));
        try {
            String layoutCode = checkJob.getLayoutCode();
            String sectionStepId = checkJob.getSectionStepId();
            String sectionId = checkJob.getSectionId();
            Map<String, String> params = checkJob.getParams();
            String compositeType = OConvertUtils.getString(params.get("compositeType"));
            int expertNum = checkJob.getAllExperts().size();
            List<Map<String, Object>> scoreGroupList = null;
            List<Map<String, Object>> scoreAllList = null;
            VoteParams voteParams = new VoteParams();
            voteParams.setLayoutCode(layoutCode);
            voteParams.setSubmitCode(checkJob.getSubmitCode());
            if ("creditSubScore".equals(compositeType)) {
                voteParams.setFieldName("c1");
                //资信子项打分
                scoreGroupList = thisCheckJob.beEvalutionOutcomeMapper.getCreditSubScoreGroupList(sectionId, sectionStepId, layoutCode);
                scoreAllList = thisCheckJob.beEvalutionOutcomeMapper.getCreditSubScoreAllList(sectionId, sectionStepId, layoutCode);
            } else {
                voteParams.setFieldName("c20");
                //资信主界面打分
                scoreGroupList = thisCheckJob.beEvalutionOutcomeMapper.getCreditMainScoreGroupList(sectionId, sectionStepId, layoutCode);
                scoreAllList = thisCheckJob.beEvalutionOutcomeMapper.getCreditMainScoreAllList(sectionId, sectionStepId, layoutCode);
            }
            if (scoreGroupList != null && !scoreGroupList.isEmpty()) {
                if (scoreGroupList.size()*expertNum != scoreAllList.size()) {
                    int num = 0;
                    String enterpriseId;
                    String evaluateItem;
                    String mapKey;
                    String valKey;
                    String title = null;
                    String content = null;
                    String ownJudge;
                    String voteOption;
                    String compMethodId = null;
                    Map<String, Object> enterpriseIdMap = new HashMap<>(16);
                    List<BeExpertVoteExt> expertVoteList = new ArrayList<>();
                    BeExpertVoteExt expertVoteExt = null;
                    List<BeExpertVoteDetailExt> voteDetailList;
                    BeExpertVoteDetailExt voteDetailExt = null;
                    StringBuilder option;
                    Map<String, Object> optionMap ;
                    for (Map<String, Object> scoreGroup : scoreGroupList) {
                        optionMap = new HashMap<>(8);
                        num = OConvertUtils.getInt(scoreGroup.get("num"));
                        if (num != expertNum) {
                            enterpriseId = OConvertUtils.getString(scoreGroup.get("enterpriseId"));
                            evaluateItem = OConvertUtils.getString(scoreGroup.get("evaluateItem"));
                            mapKey = String.format("%s_%s", enterpriseId, evaluateItem);
                            if (!enterpriseIdMap.containsKey(mapKey)) {
                                option = new StringBuilder();
                                voteDetailList = new ArrayList<>();
                                for (Map<String, Object> scoreAll : scoreAllList) {
                                    valKey = OConvertUtils.getString(scoreAll.get("valKey"));
                                    if (scoreAll.containsValue(valKey) && mapKey.equals(valKey)) {
                                        title = OConvertUtils.getString(scoreAll.get("title"));
                                        content = OConvertUtils.getString(scoreAll.get("content"));
                                        ownJudge = OConvertUtils.getString(scoreAll.get("ownJudge"));
                                        voteOption = OConvertUtils.getString(scoreAll.get("voteOption"));
                                        compMethodId = OConvertUtils.getString(scoreAll.get("compMethodId"));
                                        voteDetailExt = new BeExpertVoteDetailExt();
                                        voteDetailExt.setExpertId(OConvertUtils.getString(scoreAll.get("expertId")));
                                        voteDetailExt.setOwnJudge(ownJudge);
                                        voteDetailList.add(voteDetailExt);
                                        if (optionMap.containsKey(voteOption)) {
                                            continue;
                                        }
                                        optionMap.put(voteOption, voteOption);
                                        if (option.length() == 0) {
                                            option.append(String.format("[", voteOption));
                                        } else {
                                            option.append(String.format(",", voteOption));
                                        }
                                        option.append(String.format("\"%s分\"", voteOption));
                                    }
                                }
                                option.append("]");
                                voteParams.setEnterpriseId(enterpriseId);
                                voteParams.setCompMethodId(compMethodId);
                                voteParams.setSectionId(sectionId);
                                voteParams.setSectionStepId(sectionStepId);
                                enterpriseIdMap.put(mapKey, enterpriseId);
                                expertVoteExt = new BeExpertVoteExt();
                                expertVoteExt.setTitle(title);
                                expertVoteExt.setContent(content);
                                expertVoteExt.setSectionId(sectionId);
                                expertVoteExt.setSectionStepId(sectionStepId);
                                expertVoteExt.setVoteAlgorithm(VoteAlgorithmEnum.YiPiaoFouJue.getCode());
                                expertVoteExt.setVoteType(VoteTypeEnum.Automatic.getCode());
                                expertVoteExt.setVoteOptions(option.toString());
                                expertVoteExt.setVoteParams(GsonUtil.toJson(voteParams));
                                expertVoteExt.setNextJob("updateSorceCallback");
                                expertVoteExt.setVoteDetailList(voteDetailList);
                                expertVoteList.add(expertVoteExt);
                            }
                        }
                    }

                   thisCheckJob.beExpertVoteService.createVote(expertVoteList);
//                    throw new Exception("资信标打分不一致，请进行表决！");
                }
            }
        }catch (Exception e){
            log.error("LOG[]check[]{},error={},params={}","检查资信标打分异常",GsonUtil.toJsonWithGson(e),GsonUtil.toJsonWithGson(checkJob));
            throw e;
        }
    }
}
